package san.web.article;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import san.entity.article.Article;
import san.entity.article.ArticleItem;
import san.service.article.ArticleService;
import com.utils.CollectionUtils;
import com.utils.date.DateUtils;
import com.utils.date.FormatConstants;
import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

@ParentPackage("fileUpload")
@Results( {
		@Result(name = "reload", location = "article.shtml", type = "redirect"),
		@Result(name = "chain", location = "articleitem/list/${tyid}.shtml", type = "redirect"),
		@Result(name = "imageinput", location = "../scripts/kindeditor/plugins/iamge.html", type = "redirect") })
public class ArticleitemAction extends CRUDSupportAction {

	/**
	 * 文章 action
	 */
	private static final long serialVersionUID = -4790948317289883617L;
	private Page<ArticleItem> page = new Page<ArticleItem>(10);// 每页10条记录
	@Autowired
	private ArticleService articleService;

	private Article article;
	private ArticleItem articleItem;

	private File imgFile;
	private String imgFileFileName;
	private String extname;
	private List<Article> listArticle;
	private String typeForselect;
	private String tyid;

	@Override
	protected void prepareModel() throws Exception {
	}

	@Override
	public String delete() throws Exception {
		if (StringUtils.isBlank(getId())) {
			addActionError("删除失败!");
		} else {
			articleService.removeArticleItem(getId());
			addActionMessage("删除成功!");
		}
		return "chain";
	}

	@Override
	public String input() throws Exception {
		if (article == null) {
			article = new Article();
		}
		if (StringUtils.isBlank(getId())) {
			articleItem = new ArticleItem();
			articleItem.setArticle(new Article());
		} else {
			articleItem = articleService.getArticleItemById(getId());
		}
		listArticle = articleService.getListMapAllArticle();
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		if (articleItem == null) {
			articleItem = new ArticleItem();
		}
		String hql = CollectionUtils.getHql(this.getRequest(), "articleItem",
				articleItem);
		setPage(articleService.getAllArticleForPage(page, hql, getId()));
		return SUCCESS;
	}

	/*
	 * 显示某一篇文章
	 */
	public String get() throws Exception {
		articleItem = articleService.getArticleItemById(getId());
		return "get";
	}

	/*
	 * 添加或修改某一篇文章
	 */
	@Override
	public String save() throws Exception {
		if (StringUtils.isBlank(articleItem.getId())) {
			articleService.addArticleItem(article.getId(), articleItem);
		} else {
			articleService.updateArticleItem(article.getId(), articleItem);
		}
		return "reload";
	}

	/*
	 * 上传图片
	 */
	@SuppressWarnings("deprecation")
	public String inputimage() throws Exception {
		String imagename = getImgFileFileName();
		imagename = FormatConstants.DATE_TIME_FORMAT_IMAGE.format(DateUtils
				.getCurrentDateTime())
				+ imagename.substring(imagename.lastIndexOf("."), imagename
						.length());
		try {
			String newPath = getRequest().getRealPath("")
					+ "/upload/articleimage/" + imagename;
			File dir = new File(getRequest().getRealPath("")
					+ "/upload/articleimage");
			FileUtils.forceMkdir(dir);
			FileUtils.copyFile(imgFile, new File(newPath));
			addActionMessage("保存成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存失败!");
			return "imageinput";
		}
		String id = "content1";
		// String url = "http://" + getRequest().getServerName() + ":"
		// + getRequest().getServerPort() + "/upload/articleimage/"
		// + imagename;
		String url = "/upload/articleimage/" + imagename;
		String title = "100";
		String width = "100";
		String height = "100";
		String border = "0";
		return renderHtml(("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert(\""
				+ id
				+ "\",\""
				+ url
				+ "\",\""
				+ title
				+ "\",\""
				+ width
				+ "\",\"" + height + "\",\"" + border + "\");</script>"));
	}

	public void pdf() throws Exception {
		setPage(articleService.getReportPage(page,
				(articleItem == null) ? (new ArticleItem()) : articleItem,
				getRequest()));
		articleService.getPdf(getPage(), getResponse());
	}

	public void excel() throws Exception {
		setPage(articleService.getReportPage(page,
				(articleItem == null) ? (new ArticleItem()) : articleItem,
				getRequest()));
		articleService.getExcel(getPage(), getResponse());
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleItem getArticleItem() {
		return articleItem;
	}

	public void setArticleItem(ArticleItem articleItem) {
		this.articleItem = articleItem;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getExtname() {
		return extname;
	}

	public void setExtname(String extname) {
		this.extname = extname;
	}

	public List<Article> getListArticle() {
		return listArticle;
	}

	public void setListArticle(List<Article> listArticle) {
		this.listArticle = listArticle;
	}

	public String getTypeForselect() {
		return typeForselect;
	}

	public void setTypeForselect(String typeForselect) {
		this.typeForselect = typeForselect;
	}

	public String getTyid() {
		return tyid;
	}

	public void setTyid(String tyid) {
		this.tyid = tyid;
	}

}
